package StudyCode.summary.myfile;

import java.io.IOException;
import java.nio.file.FileSystems;
import java.nio.file.Files;
import java.nio.file.Path;

/**
 * 版权所有 科技与人文(www.tah1986.com)
 */
public class DeleteTemporaryFileWithShutdownHook {
	public static void main(String[] args) throws Exception {
		Path basedir = FileSystems.getDefault().getPath("E:/Copy/");
		String tmp_file_prefix = "test_";
		String tmp_file_sufix = ".txt";

		Path tmp_file = Files.createTempFile(basedir, tmp_file_prefix, tmp_file_sufix);

		Runtime.getRuntime().addShutdownHook(new Thread() {
			@Override
			public void run() {
				System.out.println("删除临时文件 ...");

				try {
					Files.delete(tmp_file);
				} catch (IOException e) {
					System.err.println(e);
				}

				System.out.println("Shutdown-hook 执行完毕...");
			}
		});

		Thread.sleep(1000);

	}
}
